package cn.stylefeng.guns.modular.time.service.impl;

import cn.stylefeng.guns.modular.time.entity.AppUser;
import cn.stylefeng.guns.modular.time.mapper.AppUserMapper;
import cn.stylefeng.guns.modular.time.model.AppUserRequest;
import cn.stylefeng.guns.modular.time.service.AppUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {

    @Override
    public Page<AppUser> findPage(AppUserRequest request) {
        int pageNo = request.getPageNo() == null ? 1 : request.getPageNo();
        int pageSize = request.getPageSize() == null ? 20 : request.getPageSize();
        Page<AppUser> page = new Page<>(pageNo, pageSize);

        LambdaQueryWrapper<AppUser> wrapper = new LambdaQueryWrapper<>();
        if (request.getAccount() != null && !request.getAccount().isEmpty()) {
            wrapper.like(AppUser::getAccount, request.getAccount());
        }
        if (request.getNickname() != null && !request.getNickname().isEmpty()) {
            wrapper.like(AppUser::getNickname, request.getNickname());
        }
        if (request.getStatusFlag() != null && !request.getStatusFlag().isEmpty()) {
            wrapper.eq(AppUser::getStatusFlag, request.getStatusFlag());
        }
        wrapper.eq(AppUser::getDelFlag, "N");
        wrapper.orderByDesc(AppUser::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public void add(AppUserRequest request) {
        AppUser entity = new AppUser();
        entity.setAccount(request.getAccount());
        entity.setPassword(request.getPassword());
        entity.setNickname(request.getNickname());
        entity.setPhone(request.getPhone());
        entity.setEmail(request.getEmail());
        entity.setStatusFlag(request.getStatusFlag() == null ? "Y" : request.getStatusFlag());
        entity.setDelFlag("N");
        entity.setCreateTime(new Date());
        entity.setCreateUser(LoginContext.me().getLoginUser() != null ? LoginContext.me().getLoginUser().getUserId() : null);
        if (request.getRegistrationId() != null && !request.getRegistrationId().isEmpty()) {
            entity.setRegistrationId(request.getRegistrationId());
        }
        if (request.getSysUserId() != null) {
            entity.setSysUserId(request.getSysUserId());
        }
        this.save(entity);
    }

    @Override
    public void edit(AppUserRequest request) {
        AppUser entity = this.getById(request.getUserId());
        if (entity == null) { return; }
        if (request.getNickname() != null) entity.setNickname(request.getNickname());
        if (request.getPhone() != null) entity.setPhone(request.getPhone());
        if (request.getEmail() != null) entity.setEmail(request.getEmail());
        if (request.getStatusFlag() != null) entity.setStatusFlag(request.getStatusFlag());
        if (request.getRegistrationId() != null && !request.getRegistrationId().isEmpty()) {
            entity.setRegistrationId(request.getRegistrationId());
        }
        if (request.getSysUserId() != null) {
            entity.setSysUserId(request.getSysUserId());
        }
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(LoginContext.me().getLoginUser()!=null ? LoginContext.me().getLoginUser().getUserId() : null);
        this.updateById(entity);
    }

    @Override
    public void delete(AppUserRequest request) {
        AppUser entity = this.getById(request.getUserId());
        if (entity == null) { return; }
        entity.setDelFlag("Y");
        entity.setUpdateTime(new Date());
        this.updateById(entity);
    }

    @Override
    public AppUser detail(AppUserRequest request) {
        return this.getById(request.getUserId());
    }

    @Override
    public AppUser register(AppUserRequest request) {
        AppUser exist = this.getOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getAccount, request.getAccount()));
        if (exist != null) {
            return null;
        }
        AppUser entity = new AppUser();
        entity.setAccount(request.getAccount());
        entity.setPassword(request.getPassword());
        entity.setNickname(request.getNickname());
        entity.setStatusFlag("Y");
        entity.setDelFlag("N");
        if (request.getRegistrationId() != null && !request.getRegistrationId().isEmpty()) {
            entity.setRegistrationId(request.getRegistrationId());
        }
        if (request.getSysUserId() != null) {
            entity.setSysUserId(request.getSysUserId());
        }
        entity.setCreateTime(new Date());
        this.save(entity);
        return entity;
    }

    @Override
    public AppUser login(AppUserRequest request) {
        AppUser user = this.getOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getAccount, request.getAccount())
                .eq(AppUser::getDelFlag, "N"));
        if (user == null) {
            // 不存在则新增用户（自动注册）
            AppUser entity = new AppUser();
            entity.setAccount(request.getAccount());
            entity.setPassword(request.getPassword());
            entity.setNickname(request.getNickname());
            entity.setStatusFlag("Y");
            entity.setDelFlag("N");
            if (request.getRegistrationId() != null && !request.getRegistrationId().isEmpty()) {
                entity.setRegistrationId(request.getRegistrationId());
            }
            if (request.getSysUserId() != null) {
                entity.setSysUserId(request.getSysUserId());
            }
            entity.setCreateTime(new Date());
            this.save(entity);
            user = entity;
        } else {
            // 已存在则更新用户数据（registrationId / sysUserId）
            if (request.getRegistrationId() != null && !request.getRegistrationId().isEmpty()) {
                if (user.getRegistrationId() == null || !request.getRegistrationId().equals(user.getRegistrationId())) {
                    user.setRegistrationId(request.getRegistrationId());
                    user.setUpdateTime(new Date());
                    this.updateById(user);
                }
            }
            if (request.getSysUserId() != null && (user.getSysUserId() == null || !request.getSysUserId().equals(user.getSysUserId()))) {
                user.setSysUserId(request.getSysUserId());
                user.setUpdateTime(new Date());
                this.updateById(user);
            }
        }
        return user;
    }
}